
var local ={};
$.ajax({
	  type: "GET",
	  url:"http://int.dpool.sina.com.cn/iplookup/iplookup.php?callbak=?&format=js",
	  dataType: "script",
	  success: function(data){
		  local=remote_ip_info ;
		 }
	});
//小设备
	var menuLeft = document.getElementById( 'cbp-spmenu' ),
	            showLeft = document.getElementById( 'showLeft' ),
	            body = document.body;
	
	            showLeft.onclick = function() {
	            classie.toggle( this, 'active' );
	            classie.toggle( menuLeft, 'cbp-spmenu-open' );
	            disableOther( 'showLeft' );
	        };
	
	        function disableOther( button ) {
	            if( button !== 'showLeft' ) {
	                classie.toggle( showLeft, 'disabled' );
	            }
	        }
	
	var menuItem = document.getElementById( 'cbp-spmenu' );
	menuItem.onclick = function() {
	    classie.toggle( this, 'active' );
	    classie.toggle( menuLeft, 'cbp-spmenu-open' );
	    disableOther( 'showLeft' );
	};

	//数字输入限制
	function onlyNum(obj){
		obj.value=obj.value.replace(/\D/g,'');
	} 
	//显示提示
	function formMessageShow(status, msg) {
		var pCls='', iconCls='';
		if(status == true) {
			pCls = 'text-success';
			iconCls = 'glyphicon-ok';
		} else {
			pCls = 'text-danger';
			iconCls = 'glyphicon-remove';
		}
		return '<p class="col-sm-3 test-form-message '+pCls+'"><span class="glyphicon '+iconCls+'"></span><span class="stage-msg">'+msg+'</span></p>';
	}
	//隐藏提示
	function formMessageRemove(obj) {
		obj.parent().removeClass('has-success');
		obj.parent().removeClass('has-error');
		obj.parent().parent().find('.test-form-message').remove();
	}
	//错误框模板
	function errorMsg(el, msg, className) {
		var h = '<div calss="'+className+'"><div class="alert alert-danger fade"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><p><span class="glyphicon glyphicon-info-sign"></span> '+msg+'</p></div></div>';
		if($('.alert').length == 0) {
			el.prepend(h);
	    	$(".alert").addClass('in');
	    	setTimeout(function() {
	    		$(".alert").alert('close');
	    	}, 5000);
		}
	}
	//验证手机
	function checkPhone(obj) {
		//----usertel update 2014-05-28
		//移动： 139   138   137   136   135   134   147   150   151   152   157   158   159   178   182   183   184   187   188  
		//联通： 130   131   132   155   156   185   186   145   176  
		//电信： 133   153   177   180   181   189  
		//var reg =  /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[0-9])[0-9]{8}$/;
		var reg =  /^([\d]){11}/gi;
		var msg = '请正确填写您的手机号。';
		return checkForm(obj, reg, msg);
	}
	//验证姓名
	function checkName(obj) {
		var reg = /^([\u4e00-\u9fa5]){2,10}$/;
		var msg = '请正确填写您的姓名。';
		return checkForm(obj, reg, msg);
	}
	//验证序列号
	function checkSerial(obj) {
		var reg = /(?!^\s*$)/g;
		var msg = '请正确填写序列号。';
		return checkForm(obj, reg, msg);
	}
	//验证过程
	function checkForm(obj, reg, msg) {
		var value=$.trim(obj.val()),
			msgTpl='', parentCls='', status = false;
		if(reg.test(value) == false) {
			parentCls = 'has-error';
		} else {
			parentCls = 'has-success';
			msg = '';
			status = true;
		}
		if(obj.parent().parent().find('.test-form-message').length > 0) formMessageRemove(obj);
		obj.parent().addClass(parentCls);
		msgTpl = formMessageShow(status, msg);
		obj.parent().parent().append($(msgTpl));

		return status;
	}

	//表单焦点验证
	$('#username').focus(function(){
		formMessageRemove($(this));
	});
	$('#usertel').focus(function(){
		formMessageRemove($(this));
	});
//	$('#serial').focus(function(){
//		formMessageRemove($(this));
//	});
	$('#username').blur(function(){
		checkName($(this));
    });
	$('#usertel').blur(function(){
		checkPhone($(this));
	});
//	$('#serial').blur(function(){
//		checkSerial($(this))
//	});
	
	//获取code
	$('#getcode').click(function() {
		var settime = 120, i=1, showthis,
					btn = $(this),
					resetText = btn.text();
		var d = local;
			//$.parseJSON(local);
		var subData = {
				'username': $.trim($('#username').val()),
				'usertel': $('#usertel').val(),
				'flag': btn.data('billing'),
				'incity': d.city ? d.city : '',
				'inprovince': d.province ? d.province : ''
		};
		if(!checkPhone($('#usertel'))) return;
		$.ajax({
		    url: basepath + '/sms/smssendmsg.action',
		    type: 'POST',
		    data: subData,
		    beforeSend: function(){
		    	btn.button('loading');
		    },
		    success: function(data) {
		    	var res =  eval(data)[0];
		    	if(res.suc == 0) {
		    		for(i; i<=settime; i++) {
						! function(_i) {
							setTimeout(function() {
								 if(showthis == 1) {
									 btn.text(resetText);
									 btn.removeClass('disabled').removeAttr('disabled');
								   } else {
									   showthis = settime - _i;
									   btn.text(showthis+ '秒后可再次发送');
									   btn.addClass('disabled').attr('disabled', 'disabled');
								   }
						   },_i*1000);
						}(i)
					}
		    	} else {
		    		btn.button('reset');
		    		errorMsg($('.user-info-form'), '系统发生了些错误，请稍后操作。', 'col-sm-5 col-sm-offset-3');
		    	}
		    },
		    error: function(xhr, status) {
		    	btn.button('reset');
		    	errorMsg($('.user-info-form'), '发送失败，请整理好发型重新发送验证码。('+status+')', 'col-sm-5 col-sm-offset-3');
		    }
		});
	});

